package com.hotel.po.controller;

import com.hotel.common.utils.Result;
import com.hotel.po.service.RoomService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/po/rooms")
public class RoomController {

    @Autowired
    private RoomService roomService;

    @GetMapping
    public Result<Map<String, Object>> list(@RequestParam(name = "hotelId", required = false) Long hotelId,
                                            @RequestParam(name = "page", defaultValue = "1") int page,
                                            @RequestParam(name = "size", defaultValue = "10") int size,
                                            @RequestParam(name = "search", required = false) String search) {
        PageInfo<Map<String, String>> pageInfo;
        if (search != null && !search.trim().isEmpty()) {
            pageInfo = roomService.search(search.trim(), page, size);
        } else {
            pageInfo = roomService.list(hotelId, page, size);
        }
        java.util.HashMap<String, Object> resp = new java.util.HashMap<>();
        resp.put("list", pageInfo.getList());
        resp.put("total", pageInfo.getTotal());
        return Result.success(resp);
    }

    @GetMapping("/price/{roomId}")
    public Result<java.util.Map<String, Object>> price(@PathVariable("roomId") Long roomId) {
        java.util.Map<String, Object> data = new java.util.HashMap<>();
        java.util.Map<String, String> r = roomService.findById(roomId);
        if (r != null) {
            data.put("price", Double.parseDouble(r.get("price")));
            data.put("inventory", Integer.parseInt(r.get("inventory")));
        }
        return Result.success(data);
    }

    @GetMapping("/price/{roomId}/on")
    public Result<java.util.Map<String, Object>> priceOn(@PathVariable("roomId") Long roomId,
                                                         @RequestParam("date") String date) {
        Double price = roomService.calcSeasonPrice(roomId, date);
        java.util.Map<String, Object> data = new java.util.HashMap<>();
        data.put("price", price);
        return Result.success(data);
    }

    @PostMapping
    public Result<Long> create(@RequestBody Map<String, Object> body) {
        return Result.success(roomService.create(body));
    }

    @PutMapping
    public Result<Void> update(@RequestBody Map<String, Object> body) {
        roomService.update(body);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result<Void> delete(@PathVariable("id") Long id) {
        roomService.delete(id);
        return Result.success();
    }
}


